package net.lab1024.sa.base.module.support.job.sample;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 企业微信机器人消息推送Demo
 */
@Slf4j
public class sendWx {
    private static final String WEBHOOK_URL = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=9e03ab9c-be12-4cf8-b395-00672690b2b7";

    public static void main(String[] args) {
        // 测试各种消息类型
        sendTextMessage("这是一条测试文本消息");
        sendMarkdownMessage("## 告警通知\n" +
                "> 告警时间：2024-01-20 10:30:00\n" +
                "> 告警级别：<font color=\"warning\">高危</font>\n" +
                "> 告警内容：发现异常登录行为\n");
        
        // 发送图文消息
        List<Map<String, String>> articles = new ArrayList<>();
        Map<String, String> article = new HashMap<>();
        article.put("title", "安全告警通知");
        article.put("description", "检测到高危漏洞，请及时处理");
        article.put("url", "http://your-url.com");
        article.put("picurl", "http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png");
        articles.add(article);
        sendNewsMessage(articles);
    }

    /**
     * 发送文本消息
     */
    public static void sendTextMessage(String content) {
        Map<String, Object> message = new HashMap<>();
        message.put("msgtype", "text");
        
        Map<String, Object> text = new HashMap<>();
        text.put("content", content);
        message.put("text", text);
        
        sendMessage(message);
    }

    /**
     * 发送markdown消息
     */
    public static void sendMarkdownMessage(String content) {
        Map<String, Object> message = new HashMap<>();
        message.put("msgtype", "markdown");
        
        Map<String, Object> markdown = new HashMap<>();
        markdown.put("content", content);
        message.put("markdown", markdown);
        
        sendMessage(message);
    }

    /**
     * 发送图文消息
     */
    public static void sendNewsMessage(List<Map<String, String>> articles) {
        Map<String, Object> message = new HashMap<>();
        message.put("msgtype", "news");
        
        Map<String, Object> news = new HashMap<>();
        news.put("articles", articles);
        message.put("news", news);
        
        sendMessage(message);
    }

    /**
     * 发送消息到企业微信
     */
    private static void sendMessage(Map<String, Object> message) {
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            HttpPost httpPost = new HttpPost(WEBHOOK_URL);
            httpPost.addHeader("Content-Type", "application/json; charset=utf-8");
            
            String body = JSON.toJSONString(message);
            StringEntity stringEntity = new StringEntity(body, "utf-8");
            httpPost.setEntity(stringEntity);
            
            log.info("发送消息到企业微信: {}", body);
            
            try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
                HttpEntity entity = response.getEntity();
                String result = EntityUtils.toString(entity);
                JSONObject jsonResult = JSON.parseObject(result);
                
                if (jsonResult.getInteger("errcode") == 0) {
                    log.info("消息发送成功");
                } else {
                    log.error("消息发送失败: errcode={}, errmsg={}", 
                            jsonResult.getInteger("errcode"), 
                            jsonResult.getString("errmsg"));
                }
            }
        } catch (IOException e) {
            log.error("发送消息异常", e);
        }
    }
}
